package com.iwhalecloud.bss.kite.cucc.order.flow.factory;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.constant.DcPublicConsts;
import com.iwhalecloud.bss.kite.common.constant.OrderFlowConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteIterableUtils;
import com.iwhalecloud.bss.kite.cucc.order.flow.FlowEngine;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ServiceOfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.WqConstruct;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemMapper;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author v
 * @since 2020/4/2
 */
public abstract class WqConstructFactory {

    private static Splitter splitter = Splitter.on(SymbolConsts.PIPELINE).omitEmptyStrings().trimResults();

    public static WqConstruct getWqContract(String orderItemId, String tacheId, String custId) {
        WqConstruct wqConstruct;
        OrderItemMapper orderItemMapper = SpringUtil.getBean(OrderItemMapper.class);
        OrderItem orderItem = orderItemMapper.getOrderItemByOrderItemIdAndCustId(orderItemId, custId);
        ProdInst prodInst = FlowEngine.getProdInfo(orderItem);
        if (waitDispatch(orderItem, prodInst)) {
            wqConstruct = newObject(orderItem, prodInst, tacheId, "8", custId);
        }
        else {
            wqConstruct = newObject(orderItem, prodInst, tacheId, null, custId);
        }
        return wqConstruct;
    }

    private static boolean waitDispatch(OrderItem orderItem, ProdInst prodInst) {
        if (prodInst == null) {
            return false;
        }
        DcPublic configItem = DcPublicCache.get(DcPublicConsts.DC_ORDER_WAIT_DISPATCH_STYPE, DcPublicConsts.DC_ORDER_WAIT_DISPATCH_PS_PEKY);
        if (configItem != null) {
            String prodIds = configItem.getCodea();
            String askFlows = configItem.getCodeb();
            String prodId = prodInst.getProdId();
            String serviceOfferId = orderItem.getServiceOfferId();
            Set<String> serviceOfferIds = Lists.newArrayList(splitter.split(askFlows)).stream().map(ServiceOfferCache::getServOfferIdByAskFlow).filter(Objects::nonNull).collect(Collectors.toSet());
            return KiteIterableUtils.contains(splitter.split(prodIds), prodId) && serviceOfferIds.contains(serviceOfferId);
        }
        return false;
    }

    /***
     * Description: <br>
     * 得到一个缺省的WqConstructVO实例，此实例设置好了一些基本的属性值。
     * @author yu.baocheng@iwhalecloud.com<br>
     * @taskId <br>
     * @CreateDate 2020/3/8 12:29 上午 <br>
     * @since ZSmart_BSS_COC_R3.1<br>
     * @param orderItemId:
     * @param tacheId:
     * @param custId:
     * @return: com.iwhalecloud.bss.kite.dataservice.entity.flow.WqConstruct
     */
    public static WqConstruct getDefaultWqConstruct(String orderItemId, String tacheId, String custId) {
        OrderItemMapper orderItemMapper = SpringUtil.getBean(OrderItemMapper.class);
        OrderItem orderItem = orderItemMapper.getOrderItemByOrderItemIdAndCustId(orderItemId, custId);
        ProdInst prodInst = FlowEngine.getProdInfo(orderItem);
        return newObject(orderItem, prodInst, tacheId, null, custId);
    }


    private static WqConstruct newObject(OrderItem orderItem, ProdInst prodInst, String tacheId, String state, String custId) {
        WqConstruct wqConstruct = new WqConstruct();
        // 环节实例ID
        wqConstruct.setTacheId(tacheId);
        // 订单项标识
        wqConstruct.setOrderItemId(orderItem.getOrderItemId());
        if (Objects.nonNull(prodInst)) {
            wqConstruct.setProdInstId(prodInst.getProdInstId());
            wqConstruct.setProductId(prodInst.getProdId());
            wqConstruct.setAccNbr(prodInst.getAccNum());
            wqConstruct.setRegionId(prodInst.getRegionId());
        }
         // 服务提供ID
        wqConstruct.setServiceOfferId(orderItem.getServiceOfferId());
        // 预警标志
        wqConstruct.setPrewarnFlag(OrderFlowConsts.WQ_PREWARN_FLAG_NORMAL);
         // 进入队列时间
        wqConstruct.setItime(KiteDateUtils.getCurrentDate());
        // 工单状态。'0'未派单；'1'已派单；'2'派单失败;'3'已回单; 8-待送开通
        wqConstruct.setOrderState(StringUtils.defaultIfEmpty(state, OrderFlowConsts.WQ_ORDER_STATE_UNSEND));
        wqConstruct.setCustId(custId);

        return wqConstruct;
    }

}
